import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';

class ExportCard extends StatefulWidget {
  final int cardId;
  final bool resv;
  final double cardWidth;
  final bool tips;
  final bool highlight;
  ExportCard(this.cardId, this.resv, this.cardWidth, this.tips,
      {this.highlight: false});
  @override
  _ExportCardState createState() => _ExportCardState();
}

class _ExportCardState extends State<ExportCard>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _animation;
  @override
  void initState() {
    super.initState();
    if (widget.tips) {
      _controller = AnimationController(
          vsync: this, duration: Duration(milliseconds: 1000))
        ..addListener(() {
          // 用于实时更新_animation.value
          setState(() {});
        })
        ..addStatusListener((status) {
          if (status == AnimationStatus.completed) {
            // 监听动画完成的状态 The animation is stopped at the end
            _controller.reverse();
          }
          if (status == AnimationStatus.dismissed) {
            // 监听动画结束的状态 The animation is stopped at the beginning
            _controller.forward();
          }
        });

      _animation =
          CurvedAnimation(parent: _controller, curve: Curves.fastOutSlowIn);
      _animation = Tween<double>(begin: -2, end: -3).animate(_animation);
      _controller.forward();
    }
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  Widget buildExportCard(int cardId, bool resv, double cardWidth) {
    var backGroudHeight = cardWidth * 1.6;
    return Column(
      children: [
        Container(
          height: backGroudHeight,
          width: cardWidth,
          child: Stack(
            children: [
              Container(
                  width: double.infinity,
                  height: double.infinity,
                  child: Image.asset(
                    resv
                        ? "images/mahjong/user/bottom/export_re.png"
                        : "images/mahjong/user/bottom/export_background.png",
                    fit: BoxFit.cover,
                  )),
              widget.highlight
                  ? Container(
                      width: double.infinity,
                      height: double.infinity,
                      color: Colors.blue.withOpacity(0.3),
                    )
                  : SizedBox(),
              Align(
                  alignment:
                      resv ? const Alignment(0, 0.5) : const Alignment(0, -0.5),
                  child: Container(
                      width: cardWidth * MahjongSystemConst.cardMasonryWScale,
                      height: cardWidth * MahjongSystemConst.cardMasonryHScale,
                      child: Image.asset(
                        'images/mahjong/card/south_hand_tile_' +
                            cardId.toString() +
                            '.png',
                        fit: BoxFit.cover,
                      ))),
              widget.tips
                  ? Align(
                      alignment: Alignment(0, _animation.value),
                      child: Container(
                          width:
                              cardWidth * MahjongSystemConst.cardMasonryWScale,
                          height:
                              cardWidth * MahjongSystemConst.cardMasonryHScale,
                          child: Image.asset(
                            'images/mahjong/commons/diamond.png',
                            fit: BoxFit.cover,
                          )))
                  : Container(),
            ],
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return buildExportCard(widget.cardId, widget.resv, widget.cardWidth);
  }
}
